// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RobotPathValueConverter.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Navigation.Visualization
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Media;

    using Microsoft.Robotics.Numerics;

    /// <summary>
    /// Class which converts the robot path poses to path geometry.
    /// </summary>
    [ValueConversion(typeof(List<Pose2D>), typeof(StreamGeometry))]
    public class RobotPathValueConverter : IValueConverter
    {
        /// <summary>
        /// Converts the particle poses to path geometry.
        /// </summary>
        /// <param name="value">Particle poses array.</param>
        /// <param name="targetType">PathGeometry to be created.</param>
        /// <param name="parameter">The parameter is not used.</param>
        /// <param name="culture">The parameter is not used.</param>
        /// <returns>Path geometry</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            IEnumerable<Pose2D> robotPath = (IEnumerable<Pose2D>)value;

            if (robotPath != null && robotPath.Count() > 1)
            {
                Pose2D first = robotPath.First();
                StreamGeometry geometry = new StreamGeometry();

                // Open a StreamGeometryContext that can be used to describe this StreamGeometry  
                // object's contents. 
                using (StreamGeometryContext ctx = geometry.Open())
                {
                    ctx.BeginFigure(new Point(first.Y, first.X), false /* is filled */, false /* is closed */);

                    foreach (Pose2D p in robotPath.Skip(1))
                    {
                        ctx.LineTo(new Point(p.Y, p.X), true /* is stroked */, true /* is smooth join */);
                    }
                }

                geometry.Freeze();
                return geometry;
            }

            return null;
        }

        /// <summary>
        /// This method is not implemented.
        /// </summary>
        /// <param name="value">The parameter is not used.</param>
        /// <param name="targetType">The parameter is not used.</param>
        /// <param name="parameter">The parameter is not used.</param>
        /// <param name="culture">The parameter is not used.</param>
        /// <returns>Throws not implemented exception</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
